package com.hard;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Lxk on 2020/2/13.
 */


class LFUCache {

    private static class MyCache<K, V> extends LinkedHashMap<K, V> {

        private final int limit;

        public MyCache(int limit) {
            super(16, 0.75f, true);
            this.limit = limit;
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
            return this.limit < this.size();
        }

    }

    private MyCache<Integer, Integer> cache;

    public LFUCache(int capacity) {
        cache = new MyCache<>(capacity);
    }

    public int get(int key) {
        Integer res = cache.get(key);
        if (res == null) {
            return -1;
        }
        return res;
    }

    public void put(int key, int value) {
        cache.put(key, value);
    }
}

public class Solution460 {

    public static void main(String[] args) {
        LFUCache cache = new LFUCache(2);
        cache.put(1, 1);
        cache.put(2, 2);
        System.out.println(cache.get(1));
        cache.put(3, 3);
        System.out.println(cache.get(2));
        System.out.println(cache.get(3));
        cache.put(4, 4);
        System.out.println(cache.get(1));
        System.out.println(cache.get(3));
        System.out.println(cache.get(4));
    }

}
